home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1265 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.5 KB  |  140 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: QUESTION about pointers and structures
  5. Date: 12 Jan 1996 18:19:43 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4d68nv$g4r@news.iag.net>
  8. References: <4d1rqg$bse@mirv.unsw.edu.au> <4d31u1$kdv@tahko.lpr.carel.fi>
  9. NNTP-Posting-Host: pm1-orl25.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. >gwong@cse.unsw.edu.au (Geoffrey  Wong) wrote:
  13. <snip>
  14. >>       SLiteral        *background; 
  15. >>       
  16. >>       Now SLiteral is:-
  17. >>       
  18. >>       typedef struct _slit_rec        *SLiteral,      /* Used in rlgg */
  19. >>       
  20. >>       and _slit_rec is:-
  21. >>       
  22. >>       struct _slit_rec
  23. >>{
  24. >>       char            Sign;           /* 0=negated, 1=pos, 2=determinate */
  25. >>       Relation        Rel;
  26. >>       Const           *Constants; /* During an rlgg process Constant and 
  27. Var is 
  28. >>                                   coded using the same data type. This is 
  29. >>                                   possible since Const is larger than Var! 
  30. */
  31. >>       Which           *ConstVar;      /* 0=const, 1=Var  for each Constants 
  32. */
  33. >>       int             WeakLits;       /* value up to this literal */
  34. >>       Ordering        *ArgOrders,     /* recursive lits: =, <, >, # */
  35. >>                       *SaveArgOrders; /* copy during pruning */
  36. >>       float           Bits;           /* encoding length */
  37. >>};
  38. >>Now my question is can I just say background[i]->Sign = ???? or do I need to
  39. >>allocate space for background[i] or do I need to allocate space for 
  40. *background
  41. >>Any help would be appreciated, I am rather new to c, don't know much tricks
  42. >>yet.
  43.  
  44. When you are working with pointers, you must initialize the pointer to a
  45. valid object before you attempt to dereference it.  So, yes, you need to
  46. either allocate space or set the pointer to the address of an existing object.
  47.  
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51.  
  52. struct _slit_rec   /* elements removed for simplicity */
  53.    {
  54.    char            Sign;           /* 0=negated, 1=pos, 2=determinate */
  55.    int             WeakLits;       /* value up to this literal */
  56.    float           Bits;           /* encoding length */
  57.    };
  58.  
  59. typedef struct _slit_rec        *SLiteral;      /* Used in rlgg */
  60.        
  61. void OneDimensionalDynamic( void)
  62.    {
  63.    SLiteral   background;
  64.    
  65.    /* allocate an array of struct _slit_rec */
  66.    background = malloc( 3 * sizeof(*background));
  67.    if( background == NULL)
  68.       {
  69.       fprintf( stderr, "Allocation failure.\n");
  70.       exit( EXIT_FAILURE);
  71.       }
  72.    
  73.    printf( "One dimensional dynamic version (array of objects):\n");
  74.    background[1].Sign = '-';
  75.    printf( "background[1].Sign == %c\n\n", background[1].Sign);
  76.    
  77.    /* release the memory, when no longer needed */
  78.    free( background);
  79.    }
  80.  
  81. void TwoDimensionalDynamic( void)
  82.    {
  83.    SLiteral        *background; 
  84.    int i;
  85.                                                   
  86.    /* allocate an array of SLiteral pointers */                                                  
  87.    background = malloc( 3 * sizeof(*background)); 
  88.    if( background == NULL)
  89.       {
  90.       fprintf( stderr, "Allocation failure.\n");
  91.       exit( EXIT_FAILURE);
  92.       }
  93.                                                
  94.    /* for each SLiteral pointer, allocate at least one struct _slit_rec */
  95.    for( i = 0; i < 3; i++)
  96.       {
  97.       background[i] = malloc( sizeof(*background[i]));
  98.       if( background[i] == NULL)
  99.          {
  100.          fprintf( stderr, "Allocation failure.\n");
  101.          exit( EXIT_FAILURE);
  102.          }
  103.       }
  104.       
  105.    printf( "Two dimensional dynamic version (array of pointers):\n");
  106.    background[1]->Sign = '-';
  107.    printf( "background[1]->Sign == %c\n", background[1]->Sign);
  108.    
  109.    /* if you prefer or arrays are 2 dimensional, you can use */
  110.    background[1][0].Sign = '+';
  111.    printf( "background[1][0].Sign == %c\n", background[1][0].Sign);
  112.    
  113.    /* free all memory, when no longer needed */
  114.    for( i = 0; i < 3; i++)
  115.       free( background[i]);     
  116.    free( background);
  117.    }
  118.    
  119. int main(void)
  120.    {
  121.    struct _slit_rec rec;
  122.    SLiteral         recPtr = &rec;
  123.    SLiteral        *background = &recPtr;
  124.    
  125.    background[0]->Sign = '-';
  126.    printf( "Assign to the address of an existing object\n");
  127.    printf( "background[0]->Sign[0] == %c\n\n", background[0]->Sign);
  128.                       
  129.    OneDimensionalDynamic();
  130.    TwoDimensionalDynamic();
  131.    
  132.    return 0;
  133.    }
  134.  
  135.  
  136. -- 
  137. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  138. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  139.  
  140.